public with sharing class ContactsEXT 
{
	List<Contact> contacts;
	public string filters {get;set;}
	public string pageNumbers;		
	public datetime dateFilter;
	public string whereClause = '';
	public string queryString = 'Select Id, Name, MailingStreet, MailingCity, MailingState From Contact ';
	public string queryEnd = ' Order by Name DESC ';
	
	//Pagination Variables
   	private integer counter=0;  //keeps track of the offset
   	private integer list_size=30; //sets the page size or number of rows
   	public integer total_size; //used to show user the total size of the list	
   	
   	public string getpageNumbers()
   	{
   		/*Integer pageCounter;
   		integer totalPages = (total_size/30);
   		decimal totalPagesDec = (total_size/30);
   		
   		//if(totalPagesDec > totalPages)
   		totalPages = totalPages + 1;
   		
   		if(counter == 0)
   		{
   			pageCounter = 1; 
   		}
   		else
   		{
   			pageCounter = (counter/30) + 1;
   		}  		   		
   		
   		pageNumbers = 'Page ' + string.valueOf(pageCounter) + ' of ' + string.valueOf(totalPages);  */ 		
   		return pageNumbers;
   	}   	

	public contactsEXT(ApexPages.StandardController controller)
	{
		total_size = [select count() from Contact limit 2000]; //set the total size in the constructor
	} 

        public PageReference test() {
            return null;
        }
        
   	public PageReference newLink()
   	{
     	return new PageReference('/agent/apex/vfNewContact');
	}	        
	
	public List<SelectOption> getFilter()
	{
		List<SelectOption> options = new List<SelectOption>();
		options.add(new SelectOption('All Contacts', 'All Contacts'));
		options.add(new SelectOption('Birthdays This Month', 'Birthdays This Month'));
		options.add(new SelectOption('New This Week', 'New This Week'));
		options.add(new SelectOption('Salesmen', 'Salesmen'));
		
		return options;
	}
	
	public String getFilters()
	{
		return filters;
	}
	
	public void setFilters(String filters)
	{
		this.filters = filters;
	}
	
	
	//Pagination 
   public Contact [] getContacts() {
      try {
      	
		if(filters == 'Birthdays This Month')
		{
			whereClause = 'Where Birthdate = This_Month';
		}
		else if(filters == 'New This Week')
		{
			whereClause = 'Where CreatedDate = This_Week';
		}
		else if(filters == 'Salesmen')
		{
			whereClause = 'Where RecordType.Name = \'Salesman\'';
		}

		queryEnd = 'Order by Name Limit ' + list_size + ' offset ' + counter;	
						
		if(filters == 'All Contacts')
		{
			system.debug('Querystring1 is: ' + queryString + queryEnd);
			system.debug('counter: ' + counter + ' listsize ' + list_size + ' totalsize' + total_size);
			contacts = database.query(queryString + queryEnd);
			
		}
		else
		{
			system.debug('Querystring1 is: ' + queryString + whereClause + queryEnd);
			system.debug('counter: ' + counter + ' listsize ' + list_size + ' totalsize' + total_size);
			contacts = database.query(queryString + whereClause + queryEnd);
			
		}		
 
         return contacts;
      } catch (QueryException e) {
         ApexPages.addMessages(e);   
         return null;
      }
   }
 
   public PageReference Beginning() { //user clicked beginning
      counter = 0;
      return null;
   }
 
   public PageReference Previous() { //user clicked previous button
      counter -= list_size;
      return null;
   }
 
   public PageReference Next() { //user clicked next button
      counter += list_size;
      return null;
   }
 
   public PageReference End() { //user clicked end
      counter = total_size - math.mod(total_size, list_size);
      return null;
   }
 
   public Boolean getDisablePrevious() { 
      //this will disable the previous and beginning buttons
      if (counter>0) return false; else return true;
   }
 
   public Boolean getDisableNext() { //this will disable the next and end buttons
      if (counter + list_size < total_size) return false; else return true;
   }
 
   public Integer getTotal_size() {
      return total_size;
   }
 
   public Integer getPageNumber() {
      return counter/list_size + 1;
   }
 
   public Integer getTotalPages() {
      if (math.mod(total_size, list_size) > 0) {
         return total_size/list_size + 1;
      } else {
         return (total_size/list_size);
      }
   }
   
   public void inflateTest() 
     {
    Integer j = 0;
    j++;
    j++;
    j++;
    j++;
    j++;
    j++;
    j++;
    j++;    
    j++;
    j++;
    j++;
    j++;
    j++;
    j++;
    j++;
    j++;
    j++;
    j++;
    j++;
    j++;
    j++;
    j++;
    j++;
    j++;    
    j++;
    j++;
    j++;
    j++;
    j++;
    j++;
    j++;
    j++;          
	j++;
    j++;
    j++;
    j++;
    j++;
    j++;
    j++;
    j++;    
    j++;
    j++;
    j++;
    j++;
    j++;
    j++;
    j++;
    j++; 
    j++;
    j++;
    j++;
    j++;
    j++;
    j++;
    j++;
    j++;    
    j++;
    j++;
    j++;
    j++;
    j++;
    j++;
    j++;
    j++; 
  }
}